package com.tanhua.dubbo.api;

import com.tanhua.commons.utils.ComputeUtil;
import com.tanhua.domain.db.AnalysisByDay;
import com.tanhua.domain.vo.AnalysisSummaryVo;
import com.tanhua.domain.vo.AnalysisUsersVo;
import com.tanhua.dubbo.api.db.AnalysisByDayApi;
import com.tanhua.dubbo.mapper.AnalysisByDayMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@DubboService
public class AnalysisByDayApiImpl implements AnalysisByDayApi {

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Override
    public AnalysisSummaryVo querySummary(String time) {
        AnalysisSummaryVo analysisSummaryVo = new AnalysisSummaryVo();
        Long numCumulativeUsers = analysisByDayMapper.numCumulativeUsers();
        analysisSummaryVo.setCumulativeUsers(numCumulativeUsers);  //累计用户
        String passTime = new DateTime(time).minusDays(30).toString("yyyy-MM-dd");
        Long numActivePassMonth = analysisByDayMapper.numActivePass(passTime);
        analysisSummaryVo.setActivePassMonth(numActivePassMonth);  //过去30天活跃用户
        passTime = new DateTime(time).minusDays(7).toString("yyyy-MM-dd");
        Long numActivePassWeek = analysisByDayMapper.numActivePass(passTime);
        analysisSummaryVo.setActivePassWeek(numActivePassWeek);  //过去7天活跃用户
        Long numNewUsersToday = analysisByDayMapper.numNewUsers(time);
        analysisSummaryVo.setNewUsersToday(numNewUsersToday);  //今日新增用户
        String yesterday = new DateTime(time).minusDays(1).toString("yyyy-MM-dd");
        Long numNewUsersYesterday = analysisByDayMapper.numNewUsers(yesterday);
        BigDecimal rateNewUsersToday = ComputeUtil.computeRate(numNewUsersToday.intValue(), numNewUsersYesterday.intValue());
        analysisSummaryVo.setNewUsersTodayRate(rateNewUsersToday);  //今日新增用户涨跌率，单位百分数，正数为涨，负数为跌
        Long numLoginTimesToday = analysisByDayMapper.numLoginTimesToday(time);
        analysisSummaryVo.setLoginTimesToday(numLoginTimesToday);  //今日登录次数
        Long numLoginTimesYesterday = analysisByDayMapper.numLoginTimesToday(yesterday);
        BigDecimal rateLoginTimesToday = ComputeUtil.computeRate(numLoginTimesToday.intValue(), numLoginTimesYesterday.intValue());
        analysisSummaryVo.setLoginTimesTodayRate(rateLoginTimesToday);  //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        Long numActiveUsersToday = analysisByDayMapper.numActiveUsersToday(time);
        analysisSummaryVo.setActiveUsersToday(numActiveUsersToday);  //今日活跃用户
        Long numActiveUsersYesterday = analysisByDayMapper.numLoginTimesToday(yesterday);
        BigDecimal rateActiveUsersToday = ComputeUtil.computeRate(numActiveUsersToday.intValue(), numActiveUsersYesterday.intValue());
        analysisSummaryVo.setActiveUsersTodayRate(rateActiveUsersToday);  //今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        return analysisSummaryVo;
    }

    @Override
    public void insert(AnalysisByDay analysisByDay) {
        analysisByDayMapper.insert(analysisByDay);
    }

    @Override
    public List<AnalysisUsersVo> queryUsersByYear(String sd, String ed, Integer type) {
        List<AnalysisUsersVo> list = new ArrayList<>();
        //select num_registered from tb_analysis_by_day where record_date <= '2018-01-07' and record_date >= '2018-01-01'
        //select ${col} from tb_analysis_by_day where record_date <= #{ed} and record_date >= #{sd}
        String start = new DateTime(sd).toString("yyyy-MM-dd");
        String end = new DateTime(ed).toString("yyyy-MM-dd");
        String col = null;
        if (type == 101) {
            col = "num_registered";
        } else if (type == 102) {
            col = "num_active";
        } else {
            col = "num_retention1d";
        }
        List<AnalysisByDay> analysisByDayList = analysisByDayMapper.selectByDateAndType(start, end, col);
        for (AnalysisByDay analysisByDay : analysisByDayList) {
            AnalysisUsersVo analysisUsersVo = new AnalysisUsersVo();
            analysisUsersVo.setTitle(new DateTime(analysisByDay.getRecordDate().getTime()).toString("yyyy-MM-dd"));
            analysisUsersVo.setAmount(analysisByDay.getNumRegistered() == 0 ? analysisByDay.getNumActive() == 0 ? analysisByDay.getNumRetention1d() : analysisByDay.getNumActive() : analysisByDay.getNumRegistered());
            list.add(analysisUsersVo);
        }
        return list;
    }
}
